package cn.cxy.simpleviewpagerindicator

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.PagerAdapter
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val dataCount = 5
        indicator.setCount(dataCount)
            .setViewPager(viewPager)
            .setSelectedImageResId(R.drawable.ic_test_selected)
            .setUnSelectedImageResId(R.drawable.ic_test_unselected)
            .build()
        initViewPager(dataCount)
    }

    private fun initViewPager(dataCount: Int) {
        viewPager.adapter = object : PagerAdapter() {
            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                val view = getView(position)
                container.addView(view)
                return view
            }

            override fun getItemPosition(obj: Any): Int {
                return POSITION_NONE
            }

            override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
                container.removeView(obj as View)
            }

            override fun getCount(): Int {
                return dataCount
            }

            override fun isViewFromObject(view: View, obj: Any): Boolean {
                return view === obj
            }
        }
    }

    private fun getView(position: Int): View {
        val textView = TextView(this)
        textView.text = "第${position}页"
        return textView
    }
}